package com.foreknow.view;

import com.foreknow.dao.impl.BusinessDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.model.business;
import com.foreknow.util.PropertyJtable;

import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class update extends JFrame {

	private JPanel contentPane;
	public JTextField businessname;
	public JTextField password;
	public JTextField address;
	public JTextField Explain;
	public JTextField id;
	public JTextField starPrice;
	public JTextField deliveryPrice;


	business busi=new business();
	int businessId1 ;
	String businessAddress;
	String businessExplain ;
	String businessName ;
	float deliveryPrice1 ;
	float starPrice1 ;
	String password1 ;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					update frame = new update();
					frame.setVisible(true);
					frame.setLocation(500,190);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	//updateByid upda=new updateByid();
	public update() {
		setTitle("\u4FEE\u6539");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 433);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton button = new JButton("修改");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//获取数据库连接
				DBManager instance = DBManager.getInstance();
				Connection conn = instance.getConnection();
				JdbcTemplate instance1 = JdbcTemplate.getInstance();
				instance1.setConnection(conn);
				BusinessDaoImpl businessDao=new BusinessDaoImpl();



				int newid = Integer.parseInt(id.getText());   //类型转换
				float newstarPrice = Float.parseFloat(starPrice.getText());
				float newdeliveryPrice = Float.parseFloat(deliveryPrice.getText());


				business b = getbusiness();

				//将文本框里的数据保存到business对象
				b.setBusinessId(newid);
				b.setBusinessAddress(address.getText());
				b.setBusinessExplain(Explain.getText());
				b.setBusinessName(businessname.getText());
				b.setDeliveryPrice(newdeliveryPrice);
				b.setStarPrice(newstarPrice);
				b.setPassword(password.getText());

				int i = b.getBusinessId();

				try {
					boolean save = businessDao.save(b);
					if (save){
						JOptionPane.showMessageDialog(null,"更新成功！", "success!", JOptionPane.PLAIN_MESSAGE);
						try {
							List<Object> all = businessDao.getAll();
							PropertyJtable jtable = PropertyJtable.getJtable();   //返回一个jtable对象
							String[] gethead = jtable.gethead();
							Object[][] objects = jtable.queryData(all);
							jtable.update(objects,gethead);
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}else {
						JOptionPane.showMessageDialog(null,"更新失败！", "error!", JOptionPane.PLAIN_MESSAGE);
					}
					conn.commit();
				} catch (SQLException e) {
					e.printStackTrace();
				}

			}
		});
		button.setBounds(67, 351, 93, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("取消");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				businessname.setText("");
				password.setText("");
				address.setText("");
				Explain.setText("");
				id.setText("");
				starPrice.setText("");
				deliveryPrice.setText("");
			}
		});
		button_1.setBounds(272, 351, 93, 23);
		contentPane.add(button_1);

		businessname = new JTextField();
		businessname.setBounds(181, 121, 184, 21);
		contentPane.add(businessname);
		businessname.setColumns(10);
		
		password = new JTextField();
		password.setColumns(10);
		password.setBounds(181, 78, 184, 21);
		contentPane.add(password);
		
		address = new JTextField();
		address.setColumns(10);
		address.setBounds(181, 164, 184, 21);
		contentPane.add(address);
		
		Explain = new JTextField();
		Explain.setColumns(10);
		Explain.setBounds(181, 210, 184, 21);
		contentPane.add(Explain);
		
		id = new JTextField();
		id.setColumns(10);
		id.setBounds(181, 31, 184, 21);
		contentPane.add(id);
		
		starPrice = new JTextField();
		starPrice.setColumns(10);
		starPrice.setBounds(181, 258, 184, 21);
		contentPane.add(starPrice);
		
		deliveryPrice = new JTextField();
		deliveryPrice.setColumns(10);
		deliveryPrice.setBounds(181, 300, 184, 21);
		contentPane.add(deliveryPrice);
		
		JLabel lblNewLabel = new JLabel("businessId");
		lblNewLabel.setBounds(67, 31, 96, 15);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("password");
		lblNewLabel_1.setBounds(67, 78, 96, 15);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("businessName");
		lblNewLabel_2.setBounds(67, 121, 96, 15);
		contentPane.add(lblNewLabel_2);
		
		JLabel lblBusinessaddress = new JLabel("businessAddress");
		lblBusinessaddress.setBounds(67, 164, 105, 15);
		contentPane.add(lblBusinessaddress);
		
		JLabel lblBusinessexplain = new JLabel("businessExplain");
		lblBusinessexplain.setBounds(67, 213, 96, 15);
		contentPane.add(lblBusinessexplain);
		
		JLabel lblStarprice = new JLabel("starPrice");
		lblStarprice.setBounds(67, 261, 96, 15);
		contentPane.add(lblStarprice);
		
		JLabel lblDeliveryprice = new JLabel("deliveryPrice");
		lblDeliveryprice.setBounds(67, 303, 96, 15);
		contentPane.add(lblDeliveryprice);
	}
	public business getbusiness(){
		busi.setPassword(password1);           //将查询到的数据保存到business对象返回修改
		busi.setStarPrice(starPrice1);
		busi.setDeliveryPrice(deliveryPrice1);
		busi.setBusinessName(businessName);
		busi.setBusinessExplain(businessExplain);
		busi.setBusinessAddress(businessAddress);
		busi.setBusinessId(businessId1);
		return busi;
	}
}
